
* Figure A1 - Spectator Redistribution Screens for Lucky Outcomes and Lucky Opportunities with Information about pi;
* Made in Latex;

* Figure A2 - Distribution of effort and probability of exerting more effort

* Panel A. Distribution of tasks completed in worker task;
#delimit;
use "${data}\worker.dta", replace;

drop if tasks_comp <5;
sum tasks_c; loc mean = r(mean); loc sd = r(sd);
replace tasks_comp = 35 if tasks_comp > 35 & tasks_comp != .; // Keep within 3 sd;

graph twoway histogram tasks_comp if tasks_comp>=5, start(5) w(1) density discrete color(navy) lcolor(white) lwidth(thin)
	|| function normalden(x,`mean',`sd') if tasks_comp>=5 , range(tasks_comp) lcolor(red) lpattern(dash)	
       ytitle("Density") xlabel(5(5)30 35 "{&ge}35" , format(%4.0fc)) xscale(titlegap(2))
       xtitle("Number of encryptions completed") yscale(titlegap(2)) ylabel(, angle(0) format(%4.2fc))
       legend(off) graphregion(color(white)) bgcolor(white);
       graph export "${res}\tasks-comp-hist.pdf", replace as(pdf);
	
* Panel B. Probability that the winner exerted more effort as a function of multiplier ratio;
#delimit;
insheet using "${data}\pi_hat_by_treatment.csv", clear; 

twoway line pi_hat_ea pi_hat_ep0 pi_hat_ep1 m, lcolor(maroon navy forest_green) lpattern(solid dash longdash) 
	   xtitle("Ratio between high multiplier and low multiplier")                         xlabel(1(.5)4 , format(%4.1fc)                    ) xscale(titlegap(2)) 
	   ytitle("Fraction of matches where the" "high-effort worker won (empirical {&pi})") ylabel(.5(.1)1, format(%4.2fc)  angle(0) gmin gmax)
       legend(order(1 "Ex-ante" 2 "Ex-Post: Rules-before" 3 "Ex-Post: Rules-after") col(1) ring(0) position(1) bmargin(medium))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}\pi_hat_by_treatment.pdf", replace; 
	
	
	
* Figure A3 - Histogram of tasks completed by condition;
#delimit;
use "${data}\worker.dta", replace;

gen cf = (inlist(condition,1,2));
ksmirnov tasks_comp, by(cf);

drop if trt_ep0 == 1 | trt_ep1 == 1;
drop if tasks_comp <5;
replace tasks_comp = 35 if tasks_comp > 35 & tasks_comp !=.;  // Keep within 3 sd for the plot;


twoway histogram tasks_comp if cf == 1, start(5) w(1) density discrete color(gs12)  lcolor(gs12) lwidth(thin)||
       histogram tasks_comp if cf == 0, start(5) w(1) density discrete color(none) lcolor(red)  lwidth(thin) 
       ytitle("Density") xlabel(5(5)30 35 "{&ge}35" , format(%4.0fc)) xscale(titlegap(2))
       xtitle("Number of encryptions completed") yscale(titlegap(2)) ylabel(, angle(0) format(%4.2fc))
       legend(order(1 "Lucky Outcomes" 2 "Lucky Opportunities") ring(0) pos(2) col(1)) 
	   graphregion(color(white)) bgcolor(white);
       graph export "${res}\task-hist-cf-iop.pdf", replace as(pdf);

* Figure B3 - Redistribution and awareness of rules in the ex-post lucky opportunities and lucky outcomes conditions;
#delimit;
use "${data}\spectator.dta", replace; gen n = 1;
drop if condition == "EA";
drop if info      == "yes";

collapse (mean) shr_red (sum) n (min) min_pi=pi (max) max_pi=pi (semean) semean = shr_red, by(condition rules pi_bin); gen mid_bin = (max_pi+min_pi)/2;

replace mid_bin = 100 - mid_bin;
replace mid_bin = mid_bin+0.35 if condition == "EP" & rules == "before";
replace mid_bin = mid_bin-0.35 if condition == "EP" & rules == "after" ;
replace mid_bin = mid_bin+0.35 if condition == "CF" & rules == "before";
replace mid_bin = mid_bin-0.35 if condition == "CF" & rules == "after" ;


cap drop x* y*; gen x1 = 25; gen x2 = 25; gen y1 = .45; gen y2 = .49;
cap drop w* z*; gen w1 = 25; gen w2 = 25; gen z1 = .05; gen z2 = .01;

cap drop a* b*; gen a1 = 4 ; gen a2 = 1 ; gen b1 = .45  ; gen b2 = .45;
cap drop c* d*; gen c1 = 45; gen c2 = 49; gen d1 = .045 ; gen d2 = .045;

gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

* Panel A. Ex-Post Lucky Opportunities;
#delimit;
local emdash = ustrunescape("\u2013");

twoway connect shr_red mid_bin [fw=n] if condition == "EP" & rules == "before", lcolor(forest_green)    mcolor(forest_green) msize(small) msym(S)
	|| connect shr_red mid_bin [fw=n] if condition == "EP" & rules == "after" , lcolor(orange)          mcolor(orange)       msize(small) msym(T)
	|| rcap      up lo mid_bin [fw=n] if condition == "EP" & rules == "before", lcolor(forest_green%75) lpattern(dash) lwidth(vthin)
	|| rcap      up lo mid_bin [fw=n] if condition == "EP" & rules == "after" , lcolor(orange%75      ) lpattern(dash) lwidth(vthin)
	|| pcarrow y1 x1 y2 x2, lcolor(gs12)   mcolor(gs12)   
    || pcarrow z1 w1 z2 w2, lcolor(gs12)   mcolor(gs12)   
	|| pcarrow b1 a1 b2 a2, color(maroon*0.8) 
    || pcarrow d1 c1 d2 c2, color(maroon*0.8)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of winner's earnings redistributed") ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       yline(0.5, lpattern(dash) lcolor(gs12))      text(.43 25 "Earnings equalization", color(gs8))
       yline(0.0, lpattern(dash) lcolor(gs12))      text(.07 25 "Laissez-faire", color(gs8))
       xline(50,  lpattern(dash) lcolor(maroon%50)) text(.45 9 "Performance" "determines" "winner", color(maroon%80))
       xline(0, lpattern(dash) lcolor(maroon%50))   text(.05 40 "Chance" "determines" "winner", color(maroon%80))
       legend(order(1 "Ex-Post: Rules-before" 2 "Ex-Post: Rules-after") row(1) size(small))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}\redist-ep-rules.pdf", replace; 

	   
* Panel B. Lucky Outcomes;
#delimit;
local emdash = ustrunescape("\u2013");

twoway connect shr_red mid_bin [fw=n] if condition == "CF" & rules == "before", lcolor(forest_green)    mcolor(forest_green) msize(small) msym(S)
	|| connect shr_red mid_bin [fw=n] if condition == "CF" & rules == "after" , lcolor(orange)          mcolor(orange)       msize(small) msym(T)
	|| rcap      up lo mid_bin [fw=n] if condition == "CF" & rules == "before", lcolor(forest_green%75) lpattern(dash) lwidth(vthin)
	|| rcap      up lo mid_bin [fw=n] if condition == "CF" & rules == "after" , lcolor(orange%75      ) lpattern(dash) lwidth(vthin)
	|| pcarrow y1 x1 y2 x2, lcolor(gs12)   mcolor(gs12)   
    || pcarrow z1 w1 z2 w2, lcolor(gs12)   mcolor(gs12)   
	|| pcarrow b1 a1 b2 a2, color(maroon*0.8) 
    || pcarrow d1 c1 d2 c2, color(maroon*0.8)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of winner's earnings redistributed") ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       yline(0.5, lpattern(dash) lcolor(gs12))      text(.43 25 "Earnings equalization", color(gs8))
       yline(0.0, lpattern(dash) lcolor(gs12))      text(.07 25 "Laissez-faire", color(gs8))
       xline(50,  lpattern(dash) lcolor(maroon%50)) text(.45 9 "Performance" "determines" "winner", color(maroon%80))
       xline(0, lpattern(dash) lcolor(maroon%50))   text(.05 40 "Chance" "determines" "winner", color(maroon%80))
       legend(order(1 "Lucky Outcomes: Rules-before" 2 "Lucky Outcomes: Rules-after") row(1) size(small))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}\redist-cf-rules.pdf", replace; 
	   
	   
* Figure B4 - Redistribution and awareness of rules in lucky outcomes and ex-post lucky opportunities;

#delimit;
use "${data}/spectator.dta", replace;
local my_green   "0 158 115%75";
local my_blue    "0 114 178%75"; 
local my_purple  "128 0 128%75"; 

drop if info      == "yes";
drop if rules      == "before";

gen n = 1;
collapse (mean) shr_red (sum) n (min) min_pi=pi (max) max_pi=pi (semean) semean = shr_red, by(condition pi_bin); gen mid_bin = (max_pi+min_pi)/2;

replace mid_bin = 100 - mid_bin;
replace mid_bin = mid_bin+0.35 if condition == "EA";
replace mid_bin = mid_bin-0.35 if condition == "EP";


#delimit;
cap drop x* y*; gen x1 = 25; gen x2 = 25; gen y1 = .45; gen y2 = .49;
cap drop w* z*; gen w1 = 25; gen w2 = 25; gen z1 = .05; gen z2 = .01;

cap drop a* b*; gen a1 = 4 ; gen a2 = 1 ; gen b1 = .45  ; gen b2 = .45;
cap drop c* d*; gen c1 = 45; gen c2 = 49; gen d1 = .045 ; gen d2 = .045;

#delimit;
gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

#delimit;
local emdash = ustrunescape("\u2013");
twoway connected shr_red mid_bin if condition == "CF", lcolor("`my_blue'")   lp(solid)      mcolor("`my_blue'")   msize(small) msym(O)
	|| connected shr_red mid_bin if condition == "EP", lcolor("`my_purple'") lp(solid)      mcolor("`my_purple'") msize(small) msym(D)
	|| rcap      up lo   mid_bin if condition == "CF", lcolor("`my_blue'")   lpattern(dash) lwidth(vthin)
	|| rcap      up lo   mid_bin if condition == "EP", lcolor("`my_purple'") lpattern(dash) lwidth(vthin)
	|| pcarrow y1 x1 y2 x2, lcolor(gs12)   mcolor(gs12)   
    || pcarrow z1 w1 z2 w2, lcolor(gs12)   mcolor(gs12)   
	|| pcarrow b1 a1 b2 a2, color(maroon*0.8) 
    || pcarrow d1 c1 d2 c2, color(maroon*0.8)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of winner's earnings redistributed") ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       yline(0.5, lpattern(dash) lcolor(gs12))      text(.43 25 "Earnings equalization", color(gs8))
       yline(0.0, lpattern(dash) lcolor(gs12))      text(.07 25 "Laissez-faire", color(gs8))
       xline(50,  lpattern(dash) lcolor(maroon%50)) text(.45 9 "Performance" "determines" "winner", color(maroon%80))
       xline(0, lpattern(dash) lcolor(maroon%50))   text(.05 40 "Chance" "determines" "winner", color(maroon%80))
       legend(order(1 "Lucky Outcomes" 2 "Ex-Post Lucky Opportunities") row(1))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}/redist-pi-ep-cf-rules-after.pdf", replace; 
	
	
	
* Figure B5 - Number of encryptions completed by workers across values of $\pi$;
	
#delimit;
use "${data}\spectator.dta", replace;
gen n = 1;

gen     diff_worker_tasks = .;
replace diff_worker_tasks = worker1_tasks - worker2_tasks if player1_won == 1;
replace diff_worker_tasks = worker2_tasks - worker1_tasks if player1_won == 0;

gen tasks_winner = worker1_tasks if player1_won == 1; replace tasks_winner = worker2_tasks if player1_won == 0;
gen tasks_loser  = worker1_tasks if player1_won == 0; replace tasks_winner = worker2_tasks if player1_won == 1;

order player* worker* diff*;

collapse (mean) diff_worker_tasks tasks_winner tasks_loser (sum) n (min) min_pi=pi (max) max_pi=pi (semean) sediff = diff_worker_tasks se_winner=tasks_winner se_lower=tasks_loser, by(condition pi_bin); 

gen mid_bin = (max_pi+min_pi)/2;
replace mid_bin = 100 - mid_bin;

gen up = diff_worker_tasks + 1.96*sediff;
gen lo = diff_worker_tasks - 1.96*sediff;

gen up_win = tasks_winner + 1.96*se_winner;
gen lo_win = tasks_winner - 1.96*se_winner;

gen up_los = tasks_loser + 1.96*se_lower;
gen lo_los = tasks_loser - 1.96*se_lower;

replace mid_bin = mid_bin + 0.5 if condition == "EA";
 
global my_blue   "0 114 178";
global my_purple "106 89 127";
global my_green  "0 158 115";
   
* Panel A. Tasks completed by winners and losers and winner-loser effort gap;

#delimit;
local emdash = ustrunescape("\u2013");
twoway connect tasks_winner mid_bin [fw=n] if condition == "CF", lcolor("$my_blue") lpattern(solid) mcolor("$my_blue") msize(small) msym(D)
	|| connect tasks_loser  mid_bin [fw=n] if condition == "CF", lcolor("$my_blue") lpattern(dash)  mcolor("$my_blue") msize(small) msym(O)
	|| connect tasks_winner mid_bin [fw=n] if condition == "EA", lcolor("$my_green")   lpattern(solid) mcolor("$my_green") msize(small) msym(D)
	|| connect tasks_loser  mid_bin [fw=n] if condition == "EA", lcolor("$my_green")   lpattern(dash)  mcolor("$my_green") msize(small) msym(O)	
	|| area    diff_worker  mid_bin [fw=n] if condition == "CF", fintensity(75) fcolor("$my_blue")  lcolor("$my_blue")
	|| area    diff_worker  mid_bin [fw=n] if condition == "EA", fintensity(50) fcolor("$my_green%50") lcolor("$my_green")
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Number of encryptions") ylabel(0(5)20, format(%4.0fc)  angle(0) gmin gmax)
       legend(order(1 "Tasks winner (Lucky outcomes)" 2 "Tasks loser (Lucky outcomes)" 3 "Tasks winner (Lucky opp.)" 4 "Tasks loser (Lucky opp.)" 5 "Difference Lucky outcomes" 6 "Difference Lucky opp.") row(3) size(small))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}\effort-all-pi.pdf", replace; 
	   

replace mid_bin = mid_bin - 0.5 if condition == "EA";

keep condition mid_bin diff_worker_tasks sediff;
replace condition = "_" + condition; 
reshape wide diff_worker_tasks sediff, i(mid_bin) j(condition) string;

gen diff_CF_EA = diff_worker_tasks_CF-diff_worker_tasks_EA;

gen se_diff = sqrt(sediff_CF^2 + sediff_EA);
gen up = diff_CF_EA + 1.96*se_diff;
gen lo = diff_CF_EA - 1.96*se_diff;

* Panel B:  Difference in winner-loser effort gap across conditions;

#delimit;
twoway connect diff_CF_EA mid_bin, lcolor(black)      mcolor(black)      msize(small) msym(O)
	|| rcap    up lo      mid_bin, lcolor(black%75)  lpattern(dash) lwidth(vthin)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Differences-in-differences in tasks completed") ylabel(-5(1)5, format(%4.0fc)  angle(0) gmin gmax)
       yline(0, lcolor(gs12))
	   legend(off)  graphregion(color(white)) bgcolor(white);
  	   graph export "${res}\effort-dnd-pi.pdf", replace; 
	
gen up_1pct = diff_CF_EA + 2.58*se_diff;
gen lo_1pct = diff_CF_EA - 2.58*se_diff;

list diff_worker_tasks_CF diff_worker_tasks_EA;
  
 
* Figure B6: Redistribution and differences in the winner-loser effort gap; 

#delimit;
use "${data}\spectator.dta", replace;
gen n = 1;

gen     diff_worker_tasks = .;
replace diff_worker_tasks = worker1_tasks - worker2_tasks if player1_won == 1;
replace diff_worker_tasks = worker2_tasks - worker1_tasks if player1_won == 0;

reg shr_red diff_worker_tasks, cluster(id); glo beta = _b[diff_worker_tasks];
collapse (mean) shr_red (sum) n (semean) semean = shr_red, by(condition diff_worker_tasks);

gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

keep if n >= 30;

* Panel A. Earnings redistributed and differences in encryptions completed by winners and losers;
#delimit;
twoway connect shr_red diff_worker_tasks if condition == "CF", lcolor("$my_blue")      mcolor("$my_blue")      msize(small) msym(D)
	|| connect shr_red diff_worker_tasks if condition == "EA", lcolor("$my_green")     mcolor("$my_green")        msize(small) msym(O)
	|| rcap      up lo diff_worker_tasks if condition == "CF", lcolor("$my_blue%75")   lpattern(dash) lwidth(vthin)
	|| rcap      up lo diff_worker_tasks if condition == "EA", lcolor("$my_green%75")  lpattern(dash) lwidth(vthin)
	   xtitle("Difference in tasks completed between winner and loser") xlabel(-15(5)15, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of winner's earnings redistributed")            ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       legend(order(1 "Lucky outcomes" 2 "Lucky opportunities") row(1))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}\red-diff-tasks.pdf", replace; 
    
 
* Panel B. Redistributive gap predicted by differences in winner-loser effort gap;

#delimit;
use "${data}\spectator.dta", replace;
gen n = 1;

keep if info == "no";
gen     diff_worker_tasks = .;
replace diff_worker_tasks = worker1_tasks - worker2_tasks if player1_won == 1;
replace diff_worker_tasks = worker2_tasks - worker1_tasks if player1_won == 0;

gen tasks_winner = worker1_tasks if player1_won == 1; replace tasks_winner = worker2_tasks if player1_won == 0;
gen tasks_loser  = worker1_tasks if player1_won == 0; replace tasks_winner = worker2_tasks if player1_won == 1;

order player* worker* diff*;

replace condition = "EA" if condition == "EP";
collapse (mean) shr_red diff_worker_tasks (sum) n (min) min_pi=pi (max) max_pi=pi, by(condition pi_bin); 

gen mid_bin = (max_pi+min_pi)/2;
replace mid_bin = 100 - mid_bin;

keep shr_red diff_worker_tasks mid_bin condition;
replace condition = "_" + condition; 
reshape wide shr_red diff_worker_tasks, i(mid_bin) j(condition) string;

gen diff_shr = shr_red_CF - shr_red_EA;
gen diff_CF_EA = diff_worker_tasks_CF-diff_worker_tasks_EA;
gen diff_shr_eff = diff_CF_EA*${beta};

#delimit;
local emdash = ustrunescape("\u2013");
twoway connect shr_red_CF    mid_bin , lcolor("$my_blue")    mcolor("$my_blue")      msize(small) msym(D)
	|| connect shr_red_EA    mid_bin , lcolor("$my_green")   mcolor("$my_green")     msize(small) msym(O)
	|| connect diff_shr      mid_bin , lcolor(gs12)  lpattern(dash) mcolor(gs12)  msize(small) msym(T)
	|| connect diff_shr_eff  mid_bin , lcolor(black)        lpattern(dash) mcolor(black) msize(small) msym(S)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of earnings redistributed") ylabel(-.1(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       legend(order(1 "Lucky outcomes" 2 "Lucky opportunities" 3 "Overall gap in redistribution (lucky outcomes - lucky opp.)" 4 "Gap in redistribution explained by effort differences") row(4))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}\red-effort.pdf", replace; 
